export function _ajaxTool(method, url, data) {
    return new Promise(function (resolve, reject) {
        let xhr = new XMLHttpRequest();
        // 获取x-token, 存在就是登录过
        let token = window.sessionStorage.getItem("x-token");

        if (method == "GET") {
            //对data操作形成新的url
            let str = '?';
            for (const key in data) {
                str += `${key}=${data[key]}&`
            }
            xhr.open(method, url + str.slice(0, -1))
            if (token) {
                xhr.setRequestHeader('x-token', token)
            }
            xhr.send()
        }
        else {
            let postData = ""

            xhr.open(method, url)
            xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
            //对data操作    形成    新的字符串
            for (const key in data) {
                postData += `${key}=${data[key]}&`
            }
            postData = postData.slice(0, -1);
            if (token && url != "http://website.bluej.cn/api/admin/login") {
                xhr.setRequestHeader('x-token', token)
            }
            xhr.send(postData)
        }

        xhr.addEventListener('readystatechange', function () {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    resolve(JSON.parse(xhr.responseText))
                }
                else {
                    reject('请求有问题，请检查参数或者地址是否正确')
                }

            }
        })
    })
}